package com.feizhoutuobang.shapeline.util.mail;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*; 

import org.apache.log4j.Logger;

public class SendMail {
	
	private static final Logger LOG = Logger.getLogger(SendMail.class);
	
	//要发送Mail地址
	private String mailTo = null;
	//Mail发送的起始地址
	private String mailFrom = null;
	//SMTP主机地址
	private String smtpHost = null;
	//是否采用调试方式
	private boolean debug = false;
	private String messageBasePath = null;
	//Mail主题
	private String subject;
	//Mail内容
	private String msgContent;
	private List<String> attachedFileList;
	private String mailAccount = null;
	private String mailPass = null;
	private String messageContentMimeType ="text/html; charset=utf-8";
	private String mailbccTo = null;
	private String mailccTo = null;

    public SendMail() {
    	super();
    }
    
    private void fillMail(Session session,MimeMessage msg) throws IOException, MessagingException{
    	//String fileName = null;
    	Multipart mPart = new MimeMultipart();
    	if (mailFrom != null) {
    		msg.setFrom(new InternetAddress(mailFrom));
    		LOG.debug("发送人Mail地址："+mailFrom);
        } else {
        	LOG.debug("没有指定发送人邮件地址！");
            return;
        }
        if (mailTo != null) {
            InternetAddress[] address = InternetAddress.parse(mailTo);
            msg.setRecipients(Message.RecipientType.TO, address);
            LOG.debug("收件人Mail地址："+mailTo);
        } else {
        	LOG.debug("没有指定收件人邮件地址！");
            return;
        }
   
        if (mailccTo != null) {
            InternetAddress[] ccaddress = InternetAddress.parse(mailccTo);
            LOG.debug("CCMail地址："+mailccTo);
            msg.setRecipients(Message.RecipientType.CC, ccaddress);
        }
        if (mailbccTo != null) {
            InternetAddress[] bccaddress = InternetAddress.parse(mailbccTo);
            LOG.debug("BCCMail地址："+mailbccTo);
            msg.setRecipients(Message.RecipientType.BCC, bccaddress);
        }
        msg.setSubject(subject);
        InternetAddress[] replyAddress = { new InternetAddress(mailFrom)};
        msg.setReplyTo(replyAddress);
        // create and fill the first message part
        MimeBodyPart mBodyContent = new MimeBodyPart();
        if (msgContent != null)
            mBodyContent.setContent(msgContent, messageContentMimeType);
        else
            mBodyContent.setContent("", messageContentMimeType);
        mPart.addBodyPart(mBodyContent);
        // attach the file to the message
        if (attachedFileList != null) {
            //for (Enumeration fileList = attachedFileList.elements(); fileList.hasMoreElements();) {
        	for (String fileName : attachedFileList) {
                //fileName = (String) fileList.nextElement();
                MimeBodyPart mBodyPart = new MimeBodyPart();
   
                // attach the file to the message
                FileDataSource fds = new FileDataSource(messageBasePath + fileName);
                LOG.debug("Mail发送的附件为："+messageBasePath + fileName);
                mBodyPart.setDataHandler(new DataHandler(fds));
                mBodyPart.setFileName(fileName);
                mPart.addBodyPart(mBodyPart);
            }
        }
        msg.setContent(mPart);
        msg.setSentDate(new Date());
    }
    /**
     * 此处插入方法说明。
     */
    public void init()
    {
   
    }
    /**
     * 发送e_mail
     */
    @SuppressWarnings("static-access")
	public void sendMail() throws IOException, MessagingException {
   
        Properties props = System.getProperties();
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.auth", "true");
   
        MailAuthenticator auth = new MailAuthenticator();
        auth.setMailAccount(mailAccount);
        auth.setMailPass(mailPass);
   
        Session session = Session.getInstance(props, auth);
        session.setDebug(debug);
        MimeMessage msg = new MimeMessage(session);
        Transport trans = null;
        try {
            fillMail(session,msg);
            // send the message
            trans = session.getTransport("smtp");
            trans.connect(smtpHost, mailAccount, mailPass);
   
            trans.send(msg);
            trans.close();
        } 
        finally {
            try { trans.close(); } catch (Exception e) {}
        }
        LOG.info("发送邮件成功");
    }
    
    public void setAttachedFileList(List<String> filelist)
    {
        attachedFileList = filelist;
    }
    public void setDebug(boolean debugFlag)
    {
        debug=debugFlag;
    }
    public void setMailAccount(String strAccount) {
        mailAccount = strAccount;
    }
    public void setMailbccTo(String bccto) {
        mailbccTo = bccto;
    }
    public void setMailccTo(String ccto) {
        mailccTo = ccto;
    }
    public void setMailFrom(String from)
    {
        mailFrom=from;
    }
    public void setMailPass(String strMailPass) {
        mailPass = strMailPass;
    }
    public void setMailTo(String to)
    {
        mailTo=to;
    }
    public void setMessageBasePath(String basePath)
    {
        messageBasePath=basePath;
    }
    public void setMessageContentMimeType(String mimeType)
    {
        messageContentMimeType = mimeType;
    }
    public void setMsgContent(String content)
    {
        msgContent=content;
    }
    public void setSMTPHost(String host)
    {
        smtpHost=host;
    }
    public void setSubject(String sub)
    {
        subject=sub;
    }
} 